<template>
  <el-card shadow="never" class="mb20">
    <div slot="header">
      <span>考评细则内容</span>
    </div>
    <e-table
      class="eval-table"
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :pageObj="null"
      :stripe="false"
      :spanMethod="objectSpanMethod"
    >
      <el-table-column
        slot="order"
        label="序号"
        type="index"
        :index="indexMethod"
        width="60"
        align="center"
      />
      <el-table-column slot="content" label="考评内容" align="center">
        <template
          slot-scope="scope"
        >{{ scope.row.parentName ? scope.row.parentName : scope.row.name }}</template>
      </el-table-column>
      <el-table-column v-if="showChild" slot="child" label="考评内容子项目" align="center">
        <template slot-scope="scope">{{ scope.row.parentName ? scope.row.name : null }}</template>
      </el-table-column>
      <!-- <el-table-column label="类型" slot="type" align="center" width="100">
        <template
          slot-scope="scope"
        >{{ scope.row.parentName? '基础项目' : (scope.row.moralityItemType === 1 ? '基础项目' : '加减分项目') }}</template>
      </el-table-column>-->
      <el-table-column v-if="!many" slot="eval" width="100" label="自评分" align="center">
        <template slot-scope="scope">
          <el-input
            :readOnly="type=== 'view' || from !== 'person'"
            @input="e => handleInputGrade(e, scope.row)"
            v-model.number="scope.row.itemSelfScore"
            :min="0"
            :max="scope.row.basic"
            type="number"
          />
        </template>
      </el-table-column>
    </e-table>
  </el-card>
</template>

<script>
import { indexMethod, getSession } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      type: this.$route.params.type || getSession('createEvaluationType'),
      from: this.$route.params.from || getSession('createEvaluationFrom'),
      many: this.$route.params.many || getSession('createEvaluationMany'),
      tableData: [
        {
          evalContent: '救死扶伤，全心全意为人民服务  (有2个子项目)  总分：6',
          childContent: [
            { content: '001001  加强政治理论和职业道德学习，树立救死扶伤、以病人为中心、全新全意为人民服务的宗旨意识和服务意识，大力弘扬白求恩精神。' },
            { content: '001002  增强工作责任心，热爱本职工作，坚守岗位，尽职尽责。' },
            { content: '001002  增强工作责任心，热爱本职工作，坚守岗位，尽职尽责。' }
          ],
          basic: 3,
          grade: 0
        },
        {
          evalContent: '尊重患者的权力，为患者保守医疗秘密  (有3个子项目)  总分：11',
          childContent: [
            { content: '001001  加强政治理论和职业道德学习，树立救死扶伤、以病人为中心、全新全意为人民服务的宗旨意识和服务意识，大力弘扬白求恩精神。' },
            { content: '001002  增强工作责任心，热爱本职工作，坚守岗位，尽职尽责。' },
            { content: '002003  在开展临床医务或医疗器械试验、应用新技术和有创诊疗活动中，遵守医学伦理道德，尊重患者的知情同意权。' }
          ],
          basic: 3,
          grade: 1
        }
      ],
      loading: false,
      columns: [
        { slot: 'content' },
        { slot: 'type' },
        { slot: 'child' },
        {
          width: '70px',
          label: '基础分',
          prop: 'standard'
        },
        { slot: 'eval' }
      ],
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      showChild: true
    }
  },
  methods: {
    indexMethod,
    edit(arr) {
      console.log('createTable============', arr)
      this.showChild = arr.some(v => v.parentName)
      this.tableData = [...arr]
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log('row', row, column, rowIndex, columnIndex)
      if (row.parentName) {
        if (columnIndex === 0) {
          if (rowIndex % 1 === 0) {
            // 用于设置合并开始的行号
            return {
              rowspan: row.count,
              colspan: 1
            }
          } else {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
        }
      }
    },
    // 父级拿数据
    getData() {
      let tablePost = this.tableData.every(v => v.itemSelfScore !== null)
      console.log('tablePost', tablePost)
      // 如果有一列分数未填，则调用不提交事件，不传输数据，阻断循环
      if (tablePost) {
        this.$emit('postData', 1, this.tableData)
      } else {
        this.$emit('notSubmit', 1)
      }
    },
    handleInputGrade(e, row) {
      if (e <= row.standard) {
        let arr = []
        this.tableData.map(v => {
          if (v.itemSelfScore) {
            arr.push(v.itemSelfScore)
          }
        })
        this.$emit('countGrade', arr)
      } else {
        this.$message.error('自评分不能大于基础分')
        const index = this.tableData.findIndex(v => v.moralityItemId === row.moralityItemId)
        this.$set(this.tableData[index], 'itemSelfScore', null)
      }
    },
    handleEdit(row) {
      console.log('row', row)
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await checkList(this.queryParam)

      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    }
  }
}
</script>

